package com.huluobo.a2308douyindemo.home

import android.content.Context
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.a2308douyindemo.R
import com.huluobo.a2308douyindemo.adapter.HomeNavAdapter
import com.huluobo.a2308douyindemo.adapter.HomeParentAdapter
import com.huluobo.a2308douyindemo.databinding.FragmentHomeBinding
import com.huluobo.a2308douyindemo.mvvm.BaseFragment
import com.huluobo.a2308douyindemo.search.SearchActivity
import com.huluobo.a2308douyindemo.utils.ImageUtils
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader


class HomeFragment : BaseFragment<FragmentHomeBinding, HomeViewModel>() {
    private lateinit var homeNavAdapter: HomeNavAdapter
    private lateinit var homeParentAdapter: HomeParentAdapter
    override fun initData() {
        vm.getHomeBanner()
        vm.getHomeBannerSuccess.observe(this) {
            val bannerList = mutableListOf<String>()
            val titleList = mutableListOf<String>()
            it.data.forEach {
                bannerList.add(it.imageUrl)
                titleList.add(it.name)
            }
            binding.homeBanner.setImages(bannerList)
            binding.homeBanner.setBannerTitles(titleList)
            binding.homeBanner.start()
        }

        vm.getHomeNav()
        vm.getHomeNavSuccess.observe(this) {
            homeNavAdapter.data.clear()
            homeNavAdapter.data.addAll(it.data)
            homeNavAdapter.notifyDataSetChanged()
        }

        vm.getHomeGoods()
        vm.getHomeGoodsSuccess.observe(this) {
            homeParentAdapter.data.clear()
            homeParentAdapter.data.addAll(it.data)
            homeParentAdapter.notifyDataSetChanged()
        }

        vm.getHomeBill()
        vm.getHomeBillSuccess.observe(this) {
            val adView = LayoutInflater.from(requireContext()).inflate(R.layout.dialog_bill, null)
            val adDialog = AlertDialog.Builder(requireContext()).setView(adView).show()
            val adImageView = adView.findViewById<ImageView>(R.id.dialog_bill_iv)
            adDialog.setCanceledOnTouchOutside(false)
            adView.findViewById<ImageView>(R.id.dialog_bill_close_iv).setOnClickListener {
                adDialog.dismiss()
            }
            ImageUtils.loadImage(requireContext(), it.data[0].imageUrl, adImageView)
        }
    }

    override fun initView() {
        binding.homeBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    ImageUtils.loadImage(context, path.toString(), imageView)
                }
            }
        })
        binding.homeBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)

        binding.homeNavRv.layoutManager = GridLayoutManager(requireContext(), 4)
        homeNavAdapter = HomeNavAdapter()
        binding.homeNavRv.adapter = homeNavAdapter

        binding.homeGoodsRv.layoutManager = LinearLayoutManager(requireContext())
        homeParentAdapter = HomeParentAdapter()
        binding.homeGoodsRv.adapter = homeParentAdapter


        binding.homeSearchTv.setOnClickListener {
            startAct(SearchActivity::class.java)
        }
    }
}